package com.lenovo.smarttraffic.zm_user_module;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.database.dao.UserInfoBean;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * author  张猛
 **/

public class UserCenterAdapter extends RecyclerView.Adapter<UserCenterAdapter.Holder> {
    private Context mContext;
    private List<UserInfoBean> mdata;
    private DragLayout dragLayout;

    public UserCenterAdapter(Context mContext, List<UserInfoBean> mdata) {
        this.mContext = mContext;
        this.mdata = mdata;
    }


    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.rv_item_user_center, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        holder.tv_item_name.setText("" + mdata.get(position).getPname());
        holder.tv_item_username.setText("" + mdata.get(position).getUsername());
        holder.tv_item_tell.setText("" + mdata.get(position).getPtel());
        holder.tv_item_power.setText("一般管理员" + mdata.get(position).getPsex());
        holder.tv_item_intent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mContext.startActivity(new Intent(mContext, CollectionActivity.class));
            }
        });
        if (mdata.get(position).getPsex().equals("男")) {
            holder.circleImageView.setImageResource(R.mipmap.touxiang_2);
        } else {
            holder.circleImageView.setImageResource(R.mipmap.touxiang_1);
        }
        holder.circleImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, IllegalActivity.class);
                intent.putExtra("username",mdata.get(position).getUsername());
                mContext.startActivity(intent);
            }
        });
        holder.tv_item_collection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onLis.insert(holder.getLayoutPosition(), dragLayout);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mdata.size();
    }


    class Holder extends RecyclerView.ViewHolder {
        TextView tv_item_name, tv_item_tell, tv_item_username, tv_item_collection, tv_item_power, tv_item_intent;
        CircleImageView circleImageView;

        public Holder(View itemView) {
            super(itemView);
            tv_item_name = itemView.findViewById(R.id.tv_item_name);
            tv_item_tell = itemView.findViewById(R.id.tv_item_tell);
            tv_item_username = itemView.findViewById(R.id.tv_item_username);
            circleImageView = itemView.findViewById(R.id.iv_item_avatar);
            tv_item_collection = itemView.findViewById(R.id.tv_item_collection);
            tv_item_power = itemView.findViewById(R.id.tv_item_power);
            tv_item_intent = itemView.findViewById(R.id.tv_item_intent);
            dragLayout=itemView.findViewById(R.id.dragLayout1);
        }
    }


    public interface OnLis {
        void onTop(int position, DragLayout dragLayout);

        void insert(int position, DragLayout dragLayout);

        void delete(int position, DragLayout dragLayout);
    }

    public OnLis onLis;

    public void setOnLis(OnLis onLis) {
        this.onLis = onLis;
    }
}
